// DlgRequestView.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgRequestView.h"
#include "CelsusView.h"

// CDlgRequestView dialog

IMPLEMENT_DYNAMIC(CDlgRequestView, CDialog)

CDlgRequestView::CDlgRequestView(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgRequestView::IDD, pParent)
{
	m_parent = pParent;
}

CDlgRequestView::~CDlgRequestView()
{
}

void CDlgRequestView::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EXPLORER, m_ctrlBrowser);
}


BEGIN_MESSAGE_MAP(CDlgRequestView, CDialog)
	ON_WM_SIZE()
	ON_WM_MOVE()
END_MESSAGE_MAP()


// CDlgRequestView message handlers

BOOL CDlgRequestView::OnInitDialog()
{
	CDialog::OnInitDialog();

	// set window posistion
	CRect rect( 0,0,0,0 );
	appConfig.ReadWindowLocation( _T("Request_Window"), &rect );
	if( !rect.IsRectEmpty() )
	{
		// position window
		if( rect.Height() > 0 && rect.Width() > 0 )
			MoveWindow( rect );
	}

	// initialize browser view
	InitView();

	// size the dialog
	SizeDialog();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgRequestView::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// size the dialog
	SizeDialog();
}

void CDlgRequestView::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgRequestView::PostNcDestroy()
{
	CDialog::PostNcDestroy();
	((CCelsusView*)m_parent)->m_pRequestViewDlg = NULL;
	delete this;

	CDialog::PostNcDestroy();
}

BOOL CDlgRequestView::SizeDialog(void)
{
	// check browser
	if( !m_ctrlBrowser.GetSafeHwnd() )
		return FALSE;

	CRect rectDlg;
	CRect rectCtl;

	// get dialog rect
	GetWindowRect( rectDlg );

	// IDC_EXPLORER
	m_ctrlBrowser.GetWindowRect( rectCtl );
	ScreenToClient( rectCtl );
	m_ctrlBrowser.MoveWindow( rectCtl.left, rectCtl.top, rectDlg.Width() - 15, rectDlg.Height() - 35, FALSE );

	// save window location
	CRect rect;
	GetWindowRect( rect );	
	// save loaction
	appConfig.WriteWindowLocation( _T("Request_Window"), rect );

	return TRUE;
}

void CDlgRequestView::OnMove(int x, int y)
{
	CDialog::OnMove(x, y);

	// check if we are created
	if( m_ctrlBrowser.GetSafeHwnd() )
	{
		// save window location
		CRect rect;
		GetWindowRect( rect );	
		// save loaction
		appConfig.WriteWindowLocation( _T("Request_Window"), rect );
	}
}

void CDlgRequestView::InitView(void)
{
	// navigate to show browser window
	m_ctrlBrowser.Navigate( _T("About:Blank"), NULL, NULL, NULL, NULL );
}

void CDlgRequestView::SetView(LONG nID, CString szHTML)
{
	//set window text
	CString szText;
	szText.Format( _T("Request: %.8ld"), nID );
	SetWindowText( szText );

	// clear request
	appConfig.ClearHTML( &m_ctrlBrowser );
	// set request
	appConfig.SetHTML( &m_ctrlBrowser, szHTML );
}
